<?php
/**
 * Desc:
 * Auth: Shaoming Lu
 * Date: 2018/4/9
 * Time: 18:10
 */
namespace echophp\core;

class View
{
    private $controller;
    private $action;
    private $params;

    public function __construct($controller, $action)
    {
        $this->controller = $controller;
        $this->action = $action;
    }

    public function assign($name, $value)
    {
        $this->params[$name] = $value;
    }

    /**
     * 渲染视图
     * @access  public
     * @author  Lu Shaoming<lushao1012@163.com>
     * @method  GET
     * @param $template
     * @param array $params
     */
    public function render($template, $params = [])
    {
        $this->params = array_merge($this->params, $params);
        $arr = explode('.', $template);
        $temp = '';
        foreach ($arr as $v){
            $temp .= '/'.$v;
        }
        if(count($arr) != 2){//视图格式为 控制器/视图
            exit("Template ".$temp." doesn't exists");
        }else{
            $view = APP_PATH . 'app/view/' . $arr[0] . '/' . $arr[1] . '.php';
            if(file_exists($view)){
                if($params){
                    if($this->params){
                        extract($this->params);
                    }
                }
                include ($view);
            }else{
                exit("Template ".$temp." doesn't exists");
            }
        }
    }

}